//
//  XZTabBarController.m
//  XZIBeeBo
//
//  Created by Sean on 16/4/4.
//  Copyright © 2016年 XZSean. All rights reserved.
//

#import "XZTabBarController.h"
#import "XZHomeViewController.h"
#import "XZProfileViewController.h"
#import "XZDiscoverViewController.h"
#import "XZMessageCenterViewController.h"
#import "XZNavigationController.h"
#import "XZUITabBar.h"
#import "XZComposeController.h"


@interface XZTabBarController ()<XZUITabBarDelegate>

@end

@implementation XZTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
     //添加子控制器
    [self setChildVC];
}
//设置子控制器
- (void)setChildVC{
    XZHomeViewController *home = [[XZHomeViewController alloc] init];
    [self addChildVc:home Title:@"首页" Image:@"tabbar_home" SelectedImage:@"tabbar_home_selected"];
    
    XZMessageCenterViewController *messageCenter = [[XZMessageCenterViewController alloc] init];
    [self addChildVc:messageCenter Title:@"消息" Image:@"tabbar_message_center" SelectedImage:@"tabbar_message_center_selected"];
    
    XZDiscoverViewController *disciver = [[XZDiscoverViewController alloc] init];
    [self addChildVc:disciver Title:@"发现" Image:@"tabbar_discover" SelectedImage:@"tabbar_discover_selected"];

    
    XZProfileViewController *proFile = [[XZProfileViewController alloc] init];
    [self addChildVc:proFile Title:@"我" Image:@"tabbar_profile" SelectedImage:@"tabbar_profile_selected"];
    
    
    // 2.更换系统自带的tabbar
    //    self.tabBar = [[HWTabBar alloc] init];
    XZUITabBar *tabBar = [[XZUITabBar alloc] init];
    //tabBar.delegate = self;
    [self setValue:tabBar forKeyPath:@"tabBar"];
    /*
     [self setValue:tabBar forKeyPath:@"tabBar"];相当于self.tabBar = tabBar;
     [self setValue:tabBar forKeyPath:@"tabBar"];这行代码过后，tabBar的delegate就是XZTabBar
     说明，不用再设置tabBar.delegate = self;
     */
    
    /*
     1.如果tabBar设置完delegate后，再执行下面代码修改delegate，就会报错
     tabBar.delegate = self;
     
     2.如果再次修改tabBar的delegate属性，就会报下面的错误
     错误信息：Changing the delegate of a tab bar managed by a tab bar controller is not allowed.
     错误意思：不允许修改TabBar的delegate属性(这个TabBar是被TabBarViewController所管理的)
     */
//    [self addChildViewController:home];
//    [self addChildViewController:disciver];
//    [self addChildViewController:messageCenter];
//    [self addChildViewController:proFile];
//    
    // tabBarVc.childViewControllers = @[home,disciver,messageCenter,proFile];
}

#pragma mark ---XZUITabBarDelegate
- (void)xzUITabBarDidClickPlusButton:(XZUITabBar *)tabBar{
    
    XZComposeController *omposeVC = [[XZComposeController alloc] init];
    XZNavigationController *nav = [[XZNavigationController alloc] initWithRootViewController:omposeVC ];
    [self presentViewController:nav animated:YES completion:^{
   
    }];
}

/**
 *  添加一个子控制器
 *
 *  @param childVc       子控制
 *  @param title         标题
 *  @param image         图片
 *  @param selectedImage 选中的图片
 */
- (void)addChildVc:(UIViewController *)childVc Title:(NSString *)title Image:(NSString *)image SelectedImage:(NSString *)selectedImage {
    //设置控制器的文字
    //childVc.tabBarItem.title = title; //设置tabBarItem文字
    //childVc.navigationItem.title = title;//设置navigationItem文字
    childVc.title = title;//同时设置navigationItem和navigationItem文字
    //设置控制器的图片
    childVc.tabBarItem.image = [UIImage imageNamed:image];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    /**
     *  设置文字的样式
     */
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = XZColor(123, 123, 123);
    NSMutableDictionary *selectedTextAttrs = [NSMutableDictionary dictionary];
    selectedTextAttrs[NSForegroundColorAttributeName] = [UIColor orangeColor];
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [childVc.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
    //设置控制器颜色
    //childVc.view.backgroundColor = XZRandomColor;
    
    //添加导航控制器
    //先给外面传进来的控制器包装一个导航控制器
    XZNavigationController *nav = [[XZNavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
}




@end
